(function() {
  // alert(window.location.href);
  function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  }
  var myurl = window.location.href;
  var openid = GetRequest().openid;
  wx.ready(function() {
    var title = "Hey~一起来玩幸运刮刮卡";
    var descT = "多重流量、现金红包等你来拿！";
    var desc = "多重流量、现金红包等你来拿！";
    var link = "http://wxjx.unisk.cn/wxjx/weixinshare/doShare?prizetype=" + GetRequest().prizetype + "&prizeid=" + GetRequest().id + "&shareOpenId=" + GetRequest().openId;
    var imgUrl = "http://wxjx.unisk.cn/pages/2018/wxjx/scratchcard/images/sharepic.jpg";

    //分享到朋友圈
    wx.onMenuShareTimeline({
      title: title, // 分享标题【必填】
      desc: desc, // 分享描述【必填】
      link: link, // 分享链接【必填】
      imgUrl: imgUrl, // 分享图标【必填】
      success: function() {
        // 用户确认分享后执行的回调函数
         $.ajax({
           type: "post",
           url: getServerHost() + "flow/sendFlow",
           data: {
             openid: GetRequest().openId,
             flowid: GetRequest().id
           },
           success(rs) {
             alert(rs.msg);
           },
           error() {}
         });
      },
      cancel: function() {
        // 用户取消分享后执行的回调函数
      }
    });

    //分享给好友
    wx.onMenuShareAppMessage({
      title: title, // 分享标题【必填】
      desc: descT, // 分享描述【必填】
      link: link, // 分享链接【必填】
      imgUrl: imgUrl, // 分享图标【必填】
      type: 'link', // 分享类型,music、video或link，不填默认为link【必填】
      dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
      success: function() {
        // 用户确认分享后执行的回调函数
         $.ajax({
           type: "post",
           url: getServerHost() + "flow/sendFlow",
           data: {
             openid: GetRequest().openId,
             flowid: GetRequest().id
           },
           success(rs) {
             alert(rs.msg);
           },
           error() {}
         });
        //shareNum();
      },
      cancel: function() {
        // 用户取消分享后执行的回调函数
        alert('已取消');
      }
    });

  });

  var appId = "wxf71dfa4539d2f215";
  var nonceStr = "oDxlNmsjqvV9D29r"; //随意
  var timestamp = "1420942347"; //随意

  var signature = null;

  $.ajax({
    url: "http://wxjx.unisk.cn/weixinjx/WeiXinJxApi/getSignature",
    async: false,
    dataType: "json",
    data: {
      nonceStr: nonceStr,
      timestamp: timestamp,
      url: myurl
    },
    success: function(data) {
      signature = data.signature;
      console.log(signature);
    },
    error: function() {
      signature = "error";
    }
  });
  wx.config({
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: nonceStr,
    signature: signature,
    jsApiList: ['checkJsApi', 'onMenuShareTimeline',
      'onMenuShareAppMessage', 'onMenuShareQQ',
      'onMenuShareWeibo', 'hideMenuItems', 'showMenuItems',
      'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem',
      'translateVoice', 'startRecord', 'stopRecord',
      'onRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice',
      'uploadVoice', 'downloadVoice', 'chooseImage',
      'previewImage', 'uploadImage', 'downloadImage',
      'getNetworkType', 'openLocation', 'getLocation',
      'hideOptionMenu', 'showOptionMenu', 'closeWindow',
      'scanQRCode', 'chooseWXPay', 'openProductSpecificView',
      'addCard', 'chooseCard', 'openCard'
    ]
  });
    wx.hideMenuItems({
      menuList: [
        "menuItem:share:timeline",
        "menuItem:copyUrl",
        "menuItem:share:appMessage",
        "menuItem:share:qq",
        "menuItem:share:weiboApp",
        "menuItem:favorite",
        "menuItem:share:facebook",
        "menuItem:share:QZone",
        "menuItem:editTag",
        "menuItem:delete",
        "menuItem:copyUrl",
        "menuItem:originPage",
        "menuItem:readMode",
        "menuItem:openWithQQBrowser",
        "menuItem:openWithSafari",
        "menuItem:share:email",
        "menuItem:share:brand"
      ]
    });
})();
